﻿using GatewayClient.Models;
using GatewayClient.Service;
using GatewayClient.Device;
using GatewayClient.MQ;
using Models.Common;
using Models.Device;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;

namespace GatewayClient.MQ
{
    public class ServiceMQPayEvent
    {

        /// <summary>
        /// 处理事件
        /// </summary>
        /// <param name="json"></param>
        public bool payEvent(string json)
        {

            try
            {

                Console.WriteLine("############################################################");
                Console.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff").ToString());
                Console.WriteLine("收到云mq消息:" + json);
                Console.WriteLine("############################################################");


                MQReqModel<object> httpReqModel = JsonConvert.DeserializeObject<MQReqModel<object>>(json);
                switch (httpReqModel.respEvent)
                {
                    //判断指令类型
                    case MQEventEnum.Device_Command:
                        //网关本地系统指令
                        ServiceMQ_SystemCommand.systemProcessStart(httpReqModel);
                        break;
                    case MQEventEnum.Event_HandleCommand:
                        //事件处理
                        new ServiceHandleCommand().handle(httpReqModel);
                        break;
                    case MQEventEnum.Event_Display:
                        //显示器
                        CommandLogModel commandLogModel = (CommandLogModel)httpReqModel.data;
                        if (commandLogModel.deviceModelCode == "1008")
                        {
                            new ServiceDeviceDisplay().executeDisplay1602(httpReqModel);
                        }
                        break;
                    case MQEventEnum.Device_Gateway_Time:
                        //更新本地时间
                        new ServiceSystemTime().handle(httpReqModel.data.ToString());
                        break;
                    case MQEventEnum.Device_Gateway_UpdateConfig_Value:
                        //修改配置
                        new ServiceUpGratdeConfig().updateConfig(httpReqModel);
                        break;
                    case MQEventEnum.Device_Gateway_MQTTInfo:
                        //修改MQ连接信息
                        new ServiceUpGratdeConfig().mqInfoSet(httpReqModel);
                        break;
                    default:
                        //不被支持的指令
                        handleError(httpReqModel, "不被支持的指令");
                        break;
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("payEvent： " + ex);
                return false;
            }
        }

        public void handleError(MQReqModel<object> mQReqModel, string errMsg)
        {
            //MQReqModel<object> mQReqModel = (MQReqModel<object>)obj;
            CommandLogModel commandLogModel = mQReqModel.toGatewayData;
            ModelReceverData modelDeviceData = new ModelReceverData();
            modelDeviceData.content = errMsg;
            mQReqModel.respCode = ResponseCode.System_ERROR;
            mQReqModel.respMsg = MQEventEnum.Event_State_Error + mQReqModel.respMsg;
            mQReqModel.toServerData = modelDeviceData;
            new ServiceMQ_Helper().sendMsg(mQReqModel);
        }

    }
}
